package com.hxkj.config;

import com.hxkj.domain.po.SiteUniversal;
import com.hxkj.service.UniversalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/***
 * @desc 初始化数据 把每个站点下的url配置放入redis缓存
 *
 * @author yaosy
 * @date 2018年8月14日
 * ***/
@Component
@Order(value=2)
@Slf4j
public class InitUrlDataConfig implements CommandLineRunner {

    @Override
    public void run(String... strings) throws Exception {
        insertSiteUniversal();
    }

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private UniversalService universalService;

    private static final String UNIVERAL_SITE = "UNIVERAL_SITE";


    public void insertSiteUniversal()throws Exception{
        SiteUniversal siteUniversal_ = new SiteUniversal();
        List<SiteUniversal> siteUniversal = universalService.getList(siteUniversal_);
        if(siteUniversal!=null && siteUniversal.size() >0){
            for(SiteUniversal wb : siteUniversal){
                setMap( UNIVERAL_SITE+wb.getSiteId(),wb);
            }
        }
    }


    public <T> void setMap(String key, T obj){
        redisTemplate.opsForValue().set(key,obj);
    }

}
